<?php
new addonpacker($argv[1], $argv[2], $argv[3], $argv[4]);

/**
 * @abstract 	Class to pack CS Cart addons into a nice easy to install zip file.
 * 				It does two things; copy all curr_skin_customer addon files to the skins_repo, 
 * 				then copy all /addons and /skins_repo files into a supplied dir and zip
 * @example		new addonpacker("/somedir/CSCartRoot/", "funky_addon", "/some/output/path/", true, "basic");
 * 				will pack the funky_addon addon into a zip file at /some/output/path/funky_addon.zip
 * @author 		Will Sinclair will.sinclair@sinclairtecyhnologysolutions.com
 */
class addonpacker
{
	private $target;
	private $addon;
	private $out_dir;
	private $flush_dirs;
	private $views = array(0 =>"customer", 1=>"admin", 2=>"mail");
	private $curr_skin_customer;
	private $curr_skin_admin;
	
	function addonpacker($target, $addon, $out_dir, $flush_dirs = true, $curr_skin_customer = "basic", $curr_skin_admin = "basic")
	{
		define('DS', DIRECTORY_SEPARATOR);
		$this->target 		=	$target;
		$this->addon 		= 	$addon;
		$this->out_dir 	= 	$out_dir;
		$this->flush_dirs 	= 	$flush_dirs;
		$skin = $this->determine_current_skin();
		$this->curr_skin_customer = ($skin)? $skin : $curr_skin_customer;
		$this->curr_skin_admin = $curr_skin_admin;
		
		echo "\nSkins: c-$this->curr_skin_customer, a-$this->curr_skin_admin";
		$this->pack();
	}
	
	function pack()
	{
	    echo "\nPacking: ".$this->addon."\n";
		//copy /addons to %destination%/addons
		if($this->flush_dirs)
		{
			$this->rmkdir($this->out_dir.DS."addons".DS.$this->addon);
		}
		$this->copy_r($this->target.DS."addons".DS.$this->addon, $this->out_dir.DS."addons".DS."$this->addon");

		foreach($this->views as $view)
		{
			//copy working copy current skin/addon/%addon% to /var/skinsRepo
			echo "\nCopying view from $curr_skin_customer working copy to skins_repository: $view \n";
			$dir = "var".DS."skins_repository".DS."base".DS.$view.DS."addons".DS.$this->addon;
			if($view=="customer")
			{
				$target_skins_dir = "skins".DS.$this->curr_skin_customer.DS.$view.DS."addons".DS.$this->addon;
				
				if($this->flush_dirs)
				{
					rmdir($this->target.DS.$target_skins_dir);
					$this->rmkdir($this->target.DS.$target_skins_dir);
				}
				$res = $this->copy_r($this->target.DS.$target_skins_dir, $this->target.DS.$dir);
			}else if($view=="admin")
			{
				$target_skins_dir = "skins".DS.$this->curr_skin_admin.DS.$view.DS."addons".DS.$this->addon;
				
				if($this->flush_dirs)
				{
					rmdir($this->target.DS.$target_skins_dir);
					$this->rmkdir($this->target.DS.$target_skins_dir);
				}
				$res = $this->copy_r($this->target.DS.$target_skins_dir, $this->target.DS.$dir);
			}
			//copy /var/skinsRepo/view/addons/%addon% to $destination%/var/skinsRepo/view/addons/%addon%
			echo "\nAdding view to %target%var/skins_repository: $view \n";
			$dir = "var".DS."skins_repository".DS."base".DS.$view.DS."addons".DS.$this->addon;
			if($this->flush_dirs)
			{
				rmdir($this->out_dir.DS.$dir);
				$this->rmkdir($this->out_dir.DS.$dir);
			}
			$this->copy_r($this->target.DS.$dir, $this->out_dir.DS.$dir);
		}

		//archive
		//TODO
	}

	/*------------------------------------------------------*/
	function determine_current_skin()
	{
		//TODO:
		return "robbo_black";
	}

	/**
	 * Makes directory and returns BOOL(TRUE) if exists OR made.
	 *
	 * @param  $path Path name
	 * @return bool
	 */
	function rmkdir($path, $mode = 0755) {
		$path = rtrim(preg_replace(array("/\\\\/", "/\/{2,}/"), "/", $path), "/");
		$e = explode("/", ltrim($path, "/"));
		if(substr($path, 0, 1) == "/") {
			$e[0] = "/".$e[0];
		}
		$c = count($e);
		$cp = $e[0];
		for($i = 1; $i < $c; $i++) {
			if(!is_dir($cp) && !@mkdir($cp, $mode)) {
				return false;
			}
			$cp .= "/".$e[$i];
		}
		return @mkdir($path, $mode);
	}


	function copy_r( $path, $dest, $exclude_svn = true )
	{
	    //echo"$path -> $dest\n";
		if( is_dir($path) )
		{
			@mkdir( $dest );
			$objects = scandir($path);
			if( sizeof($objects) > 0 )
			{
				foreach( $objects as $file )
				{
					if( $file == "." || $file == ".." || ($file== ".svn" && $exclude_svn))
						continue;
					// go on
					if( is_dir( $path.DS.$file ) )
					{
						$this->copy_r( $path.DS.$file, $dest.DS.$file );
					}
					else
					{
						copy( $path.DS.$file, $dest.DS.$file );
					}
				}
			}
			return true;
		}
		elseif( is_file($path) )
		{
			return copy($path, $dest);
		}
		else
		{
			return false;
		}
	}
}
?>